////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   CODE
// AUTHOR           :   Saint Peng 
// DATE CREATE      :   2006/01/05
// DESCRIPTION      :   a MAINTENANCE FORM file for the function, CODE
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/01/05   Saint Peng      1.0                 source is generated.
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.app.setup.codeSetup.web;

import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.erp.app.util.web.BeanValidator;
import com.erp.app.util.web.MultiRowValidator;
import com.erp.framework.controller.form.MultiRow;
import com.erp.framework.controller.form.SimpleMaintStyleFormTemplate;
import com.erp.framework.model.schema.TableSchema;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;

/**
* <p>A <strong>MaintenanceForm</strong> is a JavaBean which provides property
* getter and setter methods for all of the bean properties, and implements
* <code>Serializable</code>, as required by the JavaBean specification
* The main purpose for this bean is to get the user input
* from the web browsers.
*
* @author          Saint Peng 
* @version 1.0     2006/01/05
* @since           FrameWork 1.0
*
*/
public class CodeMaintenanceForm extends SimpleMaintStyleFormTemplate {

	//Header Attributes (DB Table Name: T_CODE_HDR)
	private String companyCode;                      //DB Column Name: COMPANY_CODE
	private String refMasterCode;                    //DB Column Name: REF_MASTER_CODE
	private String refDesc;                          //DB Column Name: REF_DESC
    private String ref1Desc;                         //DB Column Name: REF_1_DESC
    private String ref2Desc;                         //DB Column Name: REF_2_DESC
    private String ref3Desc;                         //DB Column Name: REF_3_DESC
	private String createUser;                       //DB Column Name: CREATE_USER
	private String createDate;                       //DB Column Name: CREATE_DATE
	private String updateUser;                       //DB Column Name: UPDATE_USER
	private String updateDate;                       //DB Column Name: UPDATE_DATE
	private String showDesc;                         //DB Column Name: SHOW_DESC

	//Child Attributes (DB Table Name: T_CODE_DTL)
	private String[] refCode;                        //DB Column Name: REF_CODE
	private String[] dtlRefDesc;                     //DB Column Name: REF_DESC
	private String[] dtlRefStatus;                   //DB Column Name: REF_STATUS
	private String[] dtlRef1;                        //DB Column Name: REF_1
	private String[] dtlRef2;                        //DB Column Name: REF_2
	private String[] dtlRef3;                        //DB Column Name: REF_3
	private String[] dtlDcInd;                       //DB Column Name: DC_IND
	private String[] dtlDcDate;                      //DB Column Name: DC_DATE
	private String[] dtlSeq;                         //DB Column Name: SEQ

	/**
	* Validate the properties that have been set for this non-HTTP request,
	* and return an <code>ActionErrors</code> object that encapsulates any
	* validation errors that have been found.  If no errors are found, return
	* <code>null</code> or an <code>ActionErrors</code> object with no
	* recorded error messages.
	* <p>
	* The default implementation attempts to forward to the HTTP version of
	* this method.
	*
	* @param      the action errors array
	* @author     Saint Peng 
	* @since      FrameWork 1.0
	*/
	public void otherValidate(ActionErrors errors) {

		validateHdr(errors);
		validateDtl(errors);
	}

	/**
	* Validate the properties that have been set for this non-HTTP request,
	* and return an <code>ActionErrors</code> object that encapsulates any
	* validation errors that have been found.  If no errors are found, return
	* <code>null</code> or an <code>ActionErrors</code> object with no
	* recorded error messages.
	* <p>
	* The default implementation attempts to forward to the HTTP version of
	* this method.
	*
	* @param      errors The errors array that is used for storing validation errors
	* @author     Saint Peng 
	* @since      FrameWork 1.0
	*/
	public void validateHdr(ActionErrors errors) {

		/*
		if (CommonUtil.isEmpty(companyCode)) {
			ActionErrorUtil.addErrorForGeneralFieldBlank(errors, "Company Code");
		}
		*/
		TableSchema ts = getTs();
		
		if (CommonUtil.isEmpty(refMasterCode))
            addErrorForBlank(errors, ts.getDisplayWebLabelByObjectId("LBL_HDR_REF_MASTER_CODE"));

		/*
		if (CommonUtil.isEmpty(issueDate)) {
			ActionErrorUtil.addErrorForGeneralFieldBlank(errors, "Issue Date");
		} else {
			if (!CommonUtil.isValidDate(issueDate)) {
				ActionErrorUtil.addErrorForInvalidDate(errors, "Issue Date");
			}
		}
		*/
		
	}
	
	private void addErrorForBlank(ActionErrors errors, String field) {
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.general.field.blank", field));
    }

	/**
	* Validate the properties that have been set for this non-HTTP request,
	* and return an <code>ActionErrors</code> object that encapsulates any
	* validation errors that have been found.  If no errors are found, return
	* <code>null</code> or an <code>ActionErrors</code> object with no
	* recorded error messages.
	* <p>
	* The default implementation attempts to forward to the HTTP version of
	* this method.
	*
	* @param      errors The errors array that is used for storing validation errors
	* @author     Saint Peng 
	* @since      FrameWork 1.0
	*/
	public void validateDtl(ActionErrors errors) {
		
        ArrayList dtlRowList = getMultiRows(CodeMaintenanceAction.TAB_IDX_DTL);
        TableSchema ts = this.getTs();
	    TableSchema[] childTsArray = this.getChildTs();
	    TableSchema dtlTs = childTsArray[CodeMaintenanceAction.TAB_IDX_DTL];
		
		
		for (int i = 0; (dtlRowList != null) && (i < dtlRowList.size()); i++) {
			CodeDtlRow row = (CodeDtlRow) dtlRowList.get(i);
			if (!CommonUtil.isEmpty(row.getIsDelete()))
                continue;
			if (CommonUtil.isEmpty(row.getRefCode())) {
				addErrorForBlank(errors, dtlTs.getDisplayWebLabelByObjectId("LBL_DTL_REF_CODE"));
			} else {
				row.setRefCode(row.getRefCode().trim());
			}
			
			
			
		}

		/*
		ArrayList dtlRowList = getMultiRows(CodeMaintenanceAction.TAB_IDX_DTL);

		for (int i = 0; (dtlRowList != null) && (i < dtlRowList.size()); i++) {
			CodeDtlRow row = (CodeDtlRow) dtlRowList.get(i);

		}
		*/
		
	}

	/**
	* Validate the properties that have been set for this non-HTTP request,
	* and return an <code>ActionErrors</code> object that encapsulates any
	* validation errors that have been found.  If no errors are found, return
	* <code>null</code> or an <code>ActionErrors</code> object with no
	* recorded error messages.
	* <p>
	* The default implementation attempts to forward to the HTTP version of
	* this method.
	*
	* @param      mapping      the action mapping
	* @param      formIn       the action form
	* @param      request      the http request
	* @param      response     the http response
	* @param      errors       the action errors array
	* @author     Saint Peng 
	* @since      FrameWork 1.0
	*/
	public void moreValidate(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		CodeMaintenanceForm form = (CodeMaintenanceForm) formIn;

		try {
			BeanValidator validator = new BeanValidator();
			validator.validateMultiRows(mapping, form, request, response, errors, form.getMultiRows(),
					new MultiRowValidator() {

						public void moreValidate(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
									HttpServletResponse response, ActionErrors errors, MultiRow row) {

						}

					}.setTabLabelObjectId("LBL_MAINT_MOD_NAME").setLineNumberProperty("refCode")
				);
		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	/**
	* Reset all bean properties to their default state.  This method
	* is called inside the reset() method.
	*
	* @param      mapping The mapping used to select this instance
	* @param      request The servlet request we are processing
	* @author     Saint Peng 
	* @since      FrameWork 1.0
	*/
	public void otherReset(ActionMapping mapping, HttpServletRequest request) {
		
	}

	/**
	* Converts to a string representing the data in this object.
	* A new <code>String</code> object is allocated and initialized to 
	* contain the character sequence currently represented by this 
	* object. This <code>String</code> is then returned. Subsequent 
	* changes to this object do not affect the contents of the 
	* <code>String</code>. 
	*
	* @return     a string representation of the object.
	* @see        java.lang.Object#toString()
	* @author     Saint Peng 
	* @since      FrameWork 1.0
	*/
	public String toString() {
		String pkStr = "PK: {companyCode = " + this.companyCode
			+ "\n, refMasterCode = " + this.refMasterCode + "}\n";
		String colStr = "COL: {refDesc = " + this.refDesc
			+ "\n, createUser = " + this.createUser
			+ "\n, createDate = " + this.createDate
			+ "\n, updateUser = " + this.updateUser
			+ "\n, updateDate = " + this.updateDate
			+ "\n, showDesc = " + this.showDesc + "}\n";
		return "CodeMaintenanceForm.toString() (header only) is:\n" + pkStr + colStr;
	}

	////////////////////////////////////////////////////////////////////////////////
	//Generated Accessors (get/set methods) for Header (CodeHdr)
	////////////////////////////////////////////////////////////////////////////////

	public String getCompanyCode() {
		return this.companyCode;
	}

	public String getRefMasterCode() {
		return this.refMasterCode;
	}

	public String getRefDesc() {
		return this.refDesc;
	}

	public String getCreateUser() {
		return this.createUser;
	}

	public String getCreateDate() {
		return this.createDate;
	}

	public String getUpdateUser() {
		return this.updateUser;
	}

	public String getUpdateDate() {
		return this.updateDate;
	}

	public String getShowDesc() {
		return this.showDesc;
	}

	public void setCompanyCode(String companyCode) {
		this.companyCode = companyCode;
	}

	public void setRefMasterCode(String refMasterCode) {
//        if(this.pageMode.equals(PAGE_MODE_CREATE)){
//            refMasterCode = refMasterCode.toUpperCase();
//        }
    	this.refMasterCode = refMasterCode;
	}

	public void setRefDesc(String refDesc) {
		this.refDesc = refDesc;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}

	public void setUpdateUser(String updateUser) {
		this.updateUser = updateUser;
	}

	public void setUpdateDate(String updateDate) {
		this.updateDate = updateDate;
	}

	public void setShowDesc(String showDesc) {
		this.showDesc = showDesc;
	}

	////////////////////////////////////////////////////////////////////////////////
	//Generated Accessors (get/set methods) for Child (CodeDtl)
	////////////////////////////////////////////////////////////////////////////////

	public String[] getRefCode() {
		return this.refCode;
	}

	public String[] getDtlRefDesc() {
		return this.dtlRefDesc;
	}

	public String[] getDtlRefStatus() {
		return this.dtlRefStatus;
	}

	public String[] getDtlRef1() {
		return this.dtlRef1;
	}

	public String[] getDtlRef2() {
		return this.dtlRef2;
	}

	public String[] getDtlRef3() {
		return this.dtlRef3;
	}

	public String[] getDtlDcInd() {
		return this.dtlDcInd;
	}

	public String[] getDtlDcDate() {
		return this.dtlDcDate;
	}

	public void setRefCode(String[] refCode) {
//        if(this.pageMode.equals(PAGE_MODE_CREATE)){
//            for (int i = 0; refCode.length > 0 && i < refCode.length ; i++){
//                
//                refCode[i] = refCode[i].toUpperCase();
//
//            }
//        }
		this.refCode = refCode;
	}

	public void setDtlRefDesc(String[] dtlRefDesc) {
		this.dtlRefDesc = dtlRefDesc;
	}

	public void setDtlRefStatus(String[] dtlRefStatus) {
		this.dtlRefStatus = dtlRefStatus;
	}

	public void setDtlRef1(String[] dtlRef1) {
		this.dtlRef1 = dtlRef1;
	}

	public void setDtlRef2(String[] dtlRef2) {
		this.dtlRef2 = dtlRef2;
	}

	public void setDtlRef3(String[] dtlRef3) {
		this.dtlRef3 = dtlRef3;
	}

	public void setDtlDcInd(String[] dtlDcInd) {
		this.dtlDcInd = dtlDcInd;
	}

	public void setDtlDcDate(String[] dtlDcDate) {
		this.dtlDcDate = dtlDcDate;
	}

    public String getRef1Desc() {
        return ref1Desc;
    }

    public void setRef1Desc(String ref1Desc) {
        this.ref1Desc = ref1Desc;
    }

    public String getRef2Desc() {
        return ref2Desc;
    }

    public void setRef2Desc(String ref2Desc) {
        this.ref2Desc = ref2Desc;
    }

    public String getRef3Desc() {
        return ref3Desc;
    }

    public void setRef3Desc(String ref3Desc) {
        this.ref3Desc = ref3Desc;
    }

	public String[] getDtlSeq() {
		return dtlSeq;
	}

	public void setDtlSeq(String[] dtlSeq) {
		this.dtlSeq = dtlSeq;
	}

	////////////////////////////////////////////////////////////////////////////////
	//Other Accessors (get/set methods)
	////////////////////////////////////////////////////////////////////////////////
	public Iterator getCreateUserValues() {
		return this.getTemplate().getUserNameValues(this, "createUser");
    }
	
	public Iterator getUpdateUserValues() {
		return this.getTemplate().getUserNameValues(this, "updateUser");
    }
}